package com.example.hospital_208.controller;

import com.example.hospital_208.po.Doctor;
import com.example.hospital_208.service.DoctorService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/doctors")
public class DoctorController {

    private final DoctorService doctorService;

    public DoctorController(DoctorService doctorService) {
        this.doctorService = doctorService;
    }

    // 查询所有医生
    @GetMapping
    public List<Doctor> getAllDoctors() {
        return doctorService.listDoctors();
    }

    // 根据ID查询医生
    @GetMapping("/{doc_id}")
    public Doctor getDoctor(@PathVariable String doc_id) {
        return doctorService.getDoctorById(doc_id);
    }

    // 新增医生
    @PostMapping
    public String addDoctor(@RequestBody Doctor doctor) {
        int result = doctorService.addDoctor(doctor);
        return result > 0 ? "医生添加成功" : "医生添加失败";
    }

    // 更新医生信息
    @PutMapping
    public String updateDoctor(@RequestBody Doctor doctor) {
        int result = doctorService.updateDoctor(doctor);
        return result > 0 ? "医生信息更新成功" : "医生信息更新失败";
    }

    // 删除医生
    @DeleteMapping("/{doc_id}")
    public String deleteDoctor(@PathVariable String doc_id) {
        int result = doctorService.removeDoctor(doc_id);
        return result > 0 ? "医生删除成功" : "医生删除失败";
    }

    // 根据用户名获取医生ID
    @GetMapping("/getDocIdByUserName")
    public String getDocIdByUserName(@RequestParam String user_name) {
        return doctorService.getDocIdByUserName(user_name);
    }

    // 根据子科室ID查询医生
    @GetMapping("/bySubDepartment/{subDepartmentId}")
    public List<Doctor> getDoctorsBySubDepartment(@PathVariable Integer subDepartmentId) {
        return doctorService.listDoctorsBySubDepartment(subDepartmentId);
    }
}